package cn.iocoder.yudao.module.erp.controller.admin.dp.dpdx;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdx.vo.DpDxPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdx.vo.DpDxRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdx.vo.DpDxSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpDxDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemDxDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpEastListDxDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpWestListDxDO;
import cn.iocoder.yudao.module.erp.service.dp.DpDxService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP东线口岸仓库制单")
@RestController
@RequestMapping("/erp/dp-dx")
@Validated
public class DpDxController {

    @Resource
    private DpDxService dpDxService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP东线口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:create')")
    public CommonResult<String> createDpDx(@Valid @RequestBody DpDxSaveReqVO createReqVO) {
        return success(dpDxService.createDpDx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP东线口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:update')")
    public CommonResult<Boolean> updateDpDx(@Valid @RequestBody DpDxSaveReqVO updateReqVO) {
        dpDxService.updateDpDx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP东线口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:delete')")
    public CommonResult<Boolean> deleteDpDx(@RequestParam("id") String id) {
        dpDxService.deleteDpDx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP东线口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:query')")
    public CommonResult<DpDxRespVO> getDpDx(@RequestParam("id") String id) {
        DpDxDO dpDx = dpDxService.getDpDx(id);
        return success(BeanUtils.toBean(dpDx, DpDxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP东线口岸仓库制单分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:query')")
    public CommonResult<PageResult<DpDxRespVO>> getDpDxPage(@Valid DpDxPageReqVO pageReqVO) {
        PageResult<DpDxDO> pageResult = dpDxService.getDpDxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpDxRespVO.class));
    }

    @GetMapping("/dpDxOuterList")
    @Operation(summary = "获得ERP东线制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:outer')")
    public CommonResult<PageResult<DpDxRespVO>> dpDxOuterList(@Valid DpDxPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpDxDO> dpDxOuterList = dpDxService.dpDxOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpDxOuterList, DpDxRespVO.class));
    }

    @GetMapping("/dpDxVehicleList")
    @Operation(summary = "获得ERP东线配车列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:outer')")
    public CommonResult<PageResult<DpDxRespVO>> dpDxVehicleList(@Valid DpDxPageReqVO pageReqVO) {
        PageResult<DpDxDO> dpDxOuterList = dpDxService.dpDxOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpDxOuterList, DpDxRespVO.class));
    }


    @PostMapping("/dpDxOutStroe")
    @Operation(summary = "ERP东线制单配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:outer')")
    public CommonResult<Boolean> dpDxOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpDxService.dpDxOutStroe(createReqVO);
        return success(true);
    }

    @GetMapping("/dpDxEnterList")
    @Operation(summary = "获得ERP东线制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpYwEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                   @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpDxEnterList = dpDxService.dpDxEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpDxEnterList, DispatchVehicleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP东线制单 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpDxExcel(@Valid DpDxPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpDxDO> list = dpDxService.getDpDxPage(pageReqVO).getList();
        // 对数据进行字段处理
        List<DpDxRespVO> processedList = list.stream().map(dpDxDO -> {
            DpDxRespVO respVO = BeanUtils.toBean(dpDxDO, DpDxRespVO.class);
            // 格式化创建时间
//            if (dpDxDO.getCreateTime() != null) {
//                respVO.setCreateTimeFormatted(dpDxDO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
            // 转换核销字段
            if (dpDxDO.getWriteOff() != null){
                respVO.setWriteOffFormatted(dpDxDO.getWriteOff() == 1 ? "是" : "否");
            }
            // 转换线路字段
            if (dpDxDO.getLine() != null) {
                respVO.setLineFormatted(dpDxDO.getLine() == 1 ? "东线" : "西线");
            }
            return respVO;
        }).collect(Collectors.toList());
        processedList.add(new DpDxRespVO());
        processedList.add(new DpDxRespVO());
        // 创建提示信息行，并将其添加到列表末尾
        DpDxRespVO footerRow = new DpDxRespVO();
        footerRow.setId("注意：" +
                "一：提货时双方当面交接清楚，如货物提走后发现损坏少货现象本公司不负责。\n" +
                "二：预期不提货者，产生一切费用及后果由客户自己承担。\n" +
                "三：易碎的货物在承运过程中发现破碎、丢失时，只赔偿丢失的货物，不赔偿破损的货物。\n" +
                "义乌库房地址:浙江省义乌市苏溪高君路7号(浙江博尼锦纶服份有限公司西)大门进去直走100米311库房 电话/Tel:15600000311(微信同号)\n" +
                "广州库房地址:广东省佛山市南海区里水镇河塱沙里水东部工业园10号南海毛中厂二期(东侧大院311库房) 电话/Te1:18500000311(微信同号)\n" +
                "新疆库房地址:新疆维吾尔自治区乌鲁木齐市沙依巴克区青葱路1048号附1号楼311库 电话/Te1:18199990311(微信同号)");
        // 将提示信息添加到列表末尾
        processedList.add(footerRow);
        // 导出 Excel
        ExcelUtils.write(response, "ERP东线制单.xls", "数据", DpDxRespVO.class, processedList);
//                        BeanUtils.toBean(processedList, DpYwRespVO.class));
    }

    @GetMapping("/export-list")
    @Operation(summary = "导出ERP东线制单东西线清单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:exportlist')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpDxExcelList(@Valid DpDxDO dpDxDO,HttpServletResponse response) throws IOException {
        //获取线路类型
        Integer  line = dpDxDO.getLine();
        List<DpEastListDxDO> dpEastList = dpDxService.getDpEastListDxListByNo(dpDxDO.getNo());
        List<DpWestListDxDO> dpWestList = dpDxService.getDpWestListDxListByNo(dpDxDO.getNo());
        if (line.equals(1)){
            // 导出 Excel东线清单
            ExcelUtils.write(response, "ERP东线制单东线清单.xls", "清单",DpEastListDxDO.class,
                    BeanUtils.toBean(dpEastList,DpEastListDxDO.class));
        }
        if (line.equals(2)){
            // 导出 Excel西线清单
            ExcelUtils.write(response, "ERP东线制单西线清单.xls", "清单",DpWestListDxDO.class,
                    BeanUtils.toBean(dpWestList,DpWestListDxDO.class));
        }
    }


    @PutMapping("/examine")
    @Operation(summary = "ERP物流东线制单审核")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:examine')")
    public CommonResult<Boolean> examineDpDx(@Valid @RequestBody DpDxDO dpDxDO){
        dpDxService.examineDpDx(dpDxDO);
        return success(true);
    }

    // ==================== 子表（ERP东线制单货号详情） ====================

    @GetMapping("/dp-item-dx/list-by-no")
    @Operation(summary = "获得ERP东线制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:detail')")
    public CommonResult<List<DpItemDxDO>> getDpItemDxListByNo(@RequestParam("no") String no) {
        return success(dpDxService.getDpItemDxListByNo(no));
    }

    // ==================== 子表（东线东线清单） ====================

    @GetMapping("/dp-east-list-dx/list-by-no")
    @Operation(summary = "获得东线东线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:detail')")
    public CommonResult<List<DpEastListDxDO>> getDpEastListDxListByNo(@RequestParam("no") String no) {
        return success(dpDxService.getDpEastListDxListByNo(no));
    }

    // ==================== 子表（东线西线清单） ====================

    @GetMapping("/dp-west-list-dx/list-by-no")
    @Operation(summary = "获得东线西线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-dx:detail')")
    public CommonResult<List<DpWestListDxDO>> getDpWestListDxListByNo(@RequestParam("no") String no) {
        return success(dpDxService.getDpWestListDxListByNo(no));
    }

}